package definite.Guide.Book.ProgressMonitor.ProgressBar;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
import javax.swing.border.TitledBorder;

public class ProgressBarDemo extends JPanel implements ActionListener,
		PropertyChangeListener {

	// field
	private JProgressBar progressBar;
	private JButton btnStart;
	private JTextArea text;
	private JScrollPane scrollPane;
	private Task task;
	private static LineNumberReader lnr = null;

	private static int countLine() {
		int numOfline = 0;
		// fine number of line

		try {
			lnr = new LineNumberReader(new FileReader(new File("data.txt")));
			lnr.skip(Long.MAX_VALUE);
			numOfline = lnr.getLineNumber() + 1;
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception",
					JOptionPane.ERROR_MESSAGE);
		} finally {
			try {
				lnr.close();
			} catch (IOException e) {

				System.out.println(e.getMessage());
			}
		}
		// end of find line number
		return numOfline;
	}

	// contructor
	public ProgressBarDemo() {
		super(new BorderLayout());
		btnStart = new JButton("Start");
		btnStart.setActionCommand("start");
		btnStart.addActionListener(this);

		progressBar = new JProgressBar(0, countLine());
		progressBar.setValue(0);
		progressBar.setStringPainted(true);

		text = new JTextArea();
		text.setLineWrap(true);
		text.setWrapStyleWord(true);
		text.setEditable(false);
		/*Random c = new Random();
		text.setBackground(new Color(c.nextFloat(), c.nextFloat(), c
				.nextFloat()));
		text.setForeground(new Color(c.nextInt(255), c.nextInt(255), c
				.nextInt(255)));*/

		scrollPane = new JScrollPane(text);
		scrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

		JPanel panel = new JPanel();
		panel.add(btnStart);
		panel.add(progressBar);

		add(panel, BorderLayout.PAGE_START);
		add(scrollPane, BorderLayout.CENTER);
		TitledBorder border =
			new TitledBorder(null, "ProgressBar Demo", TitledBorder.TOP,
					TitledBorder.CENTER, new Font("San-Serif", Font.BOLD, 14),
					new Color(new Random().nextInt(255),
							new Random().nextInt(255),
							new Random().nextInt(255)));
		setBorder(border);
	}

	// end constructor

	// define swingworker class
	class Task extends SwingWorker<Void, Void> {

		@Override
		protected Void doInBackground() throws Exception {
			int progress = 0;

			Random r = new Random();
			// setProgress(0);
			InputStreamReader in =
				new InputStreamReader(new FileInputStream("data.txt"));
			BufferedReader br = new BufferedReader(in);
			String line;
			int i = 0;
			while ((line = br.readLine()) != null) {
				i++;
				// publish(i);
				text.append(line);
				text.append("\n");
				Thread.sleep(50);
				// Thread.sleep(r.nextInt(200));

			}

			return null;
		}

		// proce

		@Override
		protected void done() {
			Toolkit.getDefaultToolkit().beep();
			btnStart.setEnabled(true);
			setCursor(null); // turn off the wait cursor
			text.append("Done!\n");
		}
	}

	// end swingworkder

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if ("progress" == evt.getPropertyName()) {
			int progress = (Integer) evt.getNewValue();
			progressBar.setValue(progress);
			/*text.append(String.format("Completed %d%% of task.\n",
					task.getProgress()));*/
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		btnStart.setEnabled(false);
		setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		// Instances of javax.swing.SwingWorker are not reusuable, so
		// we create new instances as needed.
		task = new Task();
		task.addPropertyChangeListener(this);
		task.execute();
	}

	public static void main(String[] args) {
		Runnable r = new Runnable() {

			@Override
			public void run() {
				JFrame.setDefaultLookAndFeelDecorated(true);
				JFrame frame = new JFrame("ProgressBar");
				// frame.setUndecorated(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				JComponent contentPane = new ProgressBarDemo();
				contentPane.setOpaque(true);

				frame.setContentPane(contentPane);
				frame.setLocationRelativeTo(null);
				frame.setSize(400, 300);
				frame.setVisible(true);
			}
		};

		EventQueue.invokeLater(r);
	}
}
